package com.woniuxy.woniuhis.controller;

import java.util.ArrayList;
import java.util.List;

import javax.websocket.server.PathParam;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageInfo;
import com.woniuxy.woniuhis.entity.Patient;
import com.woniuxy.woniuhis.service.PatientService;
import com.woniuxy.woniuhis.util.ResponseJson;


@Controller
@RequestMapping("/patient")
public class PatientController {
	
	@Autowired
	PatientService patientService;

	//分页显示患者信息
	@RequestMapping("/patientlist")
	@ResponseBody
	public ResponseJson patientlist(@RequestParam Integer page,@RequestParam Integer limit) {
		//分页查询所有患者信息
		ResponseJson<Patient> patientJson = null;
		try {
			patientJson = patientService.findPatientsByPage(page,limit);
			return patientJson;
		} catch (Exception e) {
			patientJson.setCode(1);
			patientJson.setMsg(e.getMessage());
			return patientJson;
		}
	}
	
	@RequestMapping("/patientAdd")
	@ResponseBody
	public String patientAdd(@RequestBody Patient patient) {
		try {
			patientService.addPatient(patient);
			return "保存成功";
		} catch (Exception e) {
			e.printStackTrace();
			return "系统维护中";
		}
	}
	
	@RequestMapping("/patientAddOrEdit")
	@ResponseBody
	public String patientAddOrEdit(@RequestBody Patient patient) {
		//添加或者更新患者信息
		try {
			patientService.addOrEditPatient(patient);
			return "操作成功";
		} catch (Exception e) {
			e.printStackTrace();
			return "系统维护中";
		}
	}
	
	@RequestMapping("patientDelete")
	@ResponseBody
	public void patientDelete(@PathParam("patientId") String patientId) {
		//传递要删除的患者id
		try {
			patientService.deletePatientById(patientId);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	//根据传入条件搜索患者
	@RequestMapping("patientSearch")
	@ResponseBody
	public ResponseJson patientSearch(@RequestBody Patient patient) {
		//根据传过来的搜索参数搜索相关患者
		ResponseJson<Patient> patientJson = null;
		try {
			patientJson = patientService.searchPatientByCondition(patient);
			return patientJson;
		} catch (Exception e) {
			patientJson.setCode(1);
			patientJson.setMsg(e.getMessage());
			return patientJson;
		}
	}
	
	//根据患者id查询患者并返回
	@RequestMapping("getPatientById")
	@ResponseBody
	public Patient getPatientById(@RequestParam("patientId") String patientId) {
		try {
			Patient patient = patientService.findPatientById(patientId);
			System.out.println(patient);
			return patient;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
}
